package com.koron.common.web.personalizedconfiguration.service;

import com.koron.bean.base.Response;
import com.koron.common.core.util.CodeTools;
import com.koron.common.web.personalizedconfiguration.bean.PersonalizedConfiguration;
import com.koron.common.web.personalizedconfiguration.bean.PersonalizedConfigurationQuery;
import com.koron.common.web.personalizedconfiguration.mapper.PersonalizedConfigurationMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 性化配置管理{@code PersonalizedConfiguration} 服务实现类
 *
 * <p>方法1-save：根据传入的id，新增或者更新单条数据</p>
 * <p>方法2-getList：根据传入的条件，返回对应的数据</p>
 *
 * @author 朱超
 * @since 2021-09-01
 */
@Service
public class PersonalizedConfigurationService {

    /** 新增或者更新单条数据
     *
     * 如果能够根据传入的id查出数据，则更新该数据；不然则新增该数据
     *
     * @param factory
     * @param personalizedConfiguration 至少包含:id，userId，page，type，config
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @TaskAnnotation("save")
    public Response save(SessionFactory factory, PersonalizedConfiguration personalizedConfiguration) {
        PersonalizedConfigurationMapper personalizedConfigurationMapper = factory.getMapper(PersonalizedConfigurationMapper.class);
        String id = personalizedConfiguration.getId();

        // 1. 根据id获取对应的数据
        PersonalizedConfiguration checkBean = personalizedConfigurationMapper.getById(id);
        // 2. 没有数据，则新增该数据
        if (checkBean == null) {
            // 2.1 先设置一个id
            personalizedConfiguration.setId(CodeTools.getCode32());
            // 2.2 新增数据
            personalizedConfigurationMapper.insert(personalizedConfiguration);
            return Response.success("新增成功");
        }
        // 3. 已有数据，则更新该数据
        else {
            personalizedConfigurationMapper.update(personalizedConfiguration);
            return Response.success("更新成功");
        }
    }

    /** 根据请求返回对应的数据
     *
     *
     * @param factory
     * @param userId
     * @param page
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @TaskAnnotation("getList")
    public Response getList(SessionFactory factory, String userId, String page) {
        PersonalizedConfigurationMapper personalizedConfigurationMapper = factory.getMapper(PersonalizedConfigurationMapper.class);
        PersonalizedConfigurationQuery personalizedConfigurationQuery = new PersonalizedConfigurationQuery();


        // 1. 构造请求实例
        personalizedConfigurationQuery.setUserId(userId);
        personalizedConfigurationQuery.setPage(page);
        // 2. 查询对应数据
        List<PersonalizedConfiguration> personalizedConfigurationList = personalizedConfigurationMapper.getList(personalizedConfigurationQuery);

        personalizedConfigurationQuery.setUserId("default");
        personalizedConfigurationQuery.setPage(page);
        personalizedConfigurationList.addAll(personalizedConfigurationMapper.getList(personalizedConfigurationQuery));

        return Response.success(personalizedConfigurationList);
    }

    @TaskAnnotation("deleteById")
    public Response deleteById(SessionFactory factory,String id){
        PersonalizedConfigurationMapper personalizedConfigurationMapper = factory.getMapper(PersonalizedConfigurationMapper.class);
        int result = personalizedConfigurationMapper.delById(id);
        return result > 0 ? Response.success("删除成功！") : Response.fail("删除失败！");
    }
}
